package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.AdderssFeignClient;
import com.hmall.common.client.SearchFeignClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.itercepter.MyIntercepter;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.UserOrder;
import com.hmall.order.service.IOrderDetailService;
import com.hmall.order.service.IOrderLogisticsService;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private SearchFeignClient feignClient;
    @Autowired
    private AdderssFeignClient adderssFeignClient;
    @Autowired
    private IOrderService orderService;

    @Autowired
    private IOrderDetailService orderDetailService;

    @Autowired
    private IOrderLogisticsService iOrderLogisticsService;
    @Override
    public Long createOrder(UserOrder userOrder) {
        //通过feign  获取item
        Item item = feignClient.findItemById(userOrder.getItemId());
        //计算总价
        Integer totalFee = Math.toIntExact(userOrder.getNum() * item.getPrice()/100);

        Order order = new Order();
        order.setTotalFee(totalFee.longValue());
        order.setPaymentType(userOrder.getPaymentType());
        order.setStatus(1);
        order.setUserId(Long.valueOf(MyIntercepter.threadLocal.get()));
        //保存到数据库
        save(order);

        //封装OrderLogistics并添加到数据库
        Address address = adderssFeignClient.findAddressById(userOrder.getAddressId());
        OrderLogistics orderLogistics = new OrderLogistics();
        orderLogistics.setOrderId(order.getId());
        orderLogistics.setContact(address.getContact());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setStreet(address.getStreet());
        orderLogistics.setMobile(address.getMobile());
        orderLogistics.setCreateTime(order.getCreateTime());
        orderLogistics.setProvince(address.getProvince());

        iOrderLogisticsService.save(orderLogistics);

        return order.getId();



    }

    public void saveOrder(Order order){



    }
}
